<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/27
 * Time: 14:32
 */

namespace app\api\controller;


use think\Db;

class Notice extends Base
{
    /**
     * 消息轮播
     * 展示数量有后台配置，按照已发布消息的发布时间倒序
     * @return \think\response\Json
     * m_id    用户ID
     */
    public function noticeList(){
        $m_id = input('m_id');
        //获取要展示的消息数量
        $limit = Db::table('up_num_config')
            ->where('status', 1)
            ->value('num');
        // 获取用户所在工厂
        $factroy_id = Db::table('up_factory_member')->where('m_id', $m_id)->value('m_factory_id');
        // 获取用户所在省
        $province = Db::table('up_factory')->where('factory_id', $factroy_id)->value('f_area');
        // 本省或总后台发布的消息
        $noticeList = Db::table('up_notice')
            ->field('n_id, n_name, n_content')
            ->where('n_status', 1)
            ->where(function  ($query) use($province) {
                $query->where('n_sheng', 0)
                    ->whereOr('n_sheng', $province);
            })
            ->limit($limit)
            ->order('n_publish_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $noticeList]);
    }
}